package team.southcat.wsyygh.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import team.southcat.wsyygh.common.result.Result;
import team.southcat.wsyygh.common.util.AdminInfoUtil;
import team.southcat.wsyygh.model.pojo.hospital.*;
import team.southcat.wsyygh.hospital.service.HospitalService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuSiYi
 * @since 2023-04-20
 */
@Api(tags = "医院管理接口")
@RestController
@RequestMapping("/admin/hospital")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    @ApiOperation("分页查询医院列表")
    @GetMapping("/getHospitalPageList/{page}/{limit}")
    public Result getHospitalPageList(@PathVariable int page, @PathVariable int limit, HospitalVo hospitalVo){
        //map包括departments total
        Page pageResult = hospitalService.getHospitalPageList(page,limit,hospitalVo);

        return Result.ok(pageResult);
    }

    @ApiOperation("根据请求获取医院信息")
    @GetMapping("/getHospitalByReq")
    public Result getHospitalByReq(HttpServletRequest request){
        String adminId = AdminInfoUtil.getAdminId(request);
        HospitalVo hospitalVo = hospitalService.getByAdminId(adminId);
        return Result.ok(hospitalVo);
    }

    @ApiOperation("上线医院")
    @PutMapping("/online/{id}")
    public Result online(@PathVariable int id){
        Hospital hospital = hospitalService.getById(id);
        hospital.setStatus(1);
        boolean flag = hospitalService.updateById(hospital);
        return flag ? Result.ok() : Result.fail().message("更新状态失败！");

    }
    @ApiOperation("下线医院")
    @PutMapping("/line/{id}")
    public Result line(@PathVariable int id){
        Hospital hospital = hospitalService.getById(id);
        hospital.setStatus(0);
        boolean flag = hospitalService.updateById(hospital);
        return flag ? Result.ok() : Result.fail().message("更新状态失败！");

    }

    @ApiOperation("根据医院ID查询医院详情")
    @GetMapping("/getHospitalById/{id}")
    public Result getHospitalById(@PathVariable("id") long id){
        HospitalVo hospitalVO = hospitalService.selectById(id);
        return hospitalVO != null ? Result.ok(hospitalVO) : Result.fail("查询医院失败");
    }


    @ApiOperation("修改医院信息")
    @PostMapping("/updateHospital")
    public Result updateHospital(@RequestBody Hospital hospital){
        System.out.println("修改医院：" + hospital);
        boolean flag = hospitalService.updateById(hospital);
        return flag ? Result.ok() : Result.fail().message("修改失败");
    }

    @ApiOperation("更改医院状态")
    @PutMapping("/changeSwitch/{id}")
    public Result changeSwitch(@PathVariable int id){
        Hospital hospital = hospitalService.getById(id);
        Integer status = hospital.getStatus();
        if(status == 1){
            status=0;
        }else if(status == 0){
            status=1;
        }
        hospital.setStatus(status);
        boolean flag = hospitalService.updateById(hospital);
        return flag ? Result.ok() : Result.fail().message("更新状态失败！");

    }

    @ApiOperation("添加医院")
    @PostMapping("/addHospital")
    public Result addHospital(@RequestBody Hospital hospital){
        Result result = hospitalService.addHospital(hospital);
        return  result;
    }
}

